<template>
	<view class="main">
		<view class="flex align between topSearch">
			<view style="width:80%;">
				<uni-search-bar v-model="searchValue" @input="input" placeholder="输入视频名称搜索">
				</uni-search-bar>
			</view>
			<view class="addBtn flex align" @click="show = true">添加<uni-icons type="plus" size="20"
					color="#fff"></uni-icons></view>
		</view>
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="name flex align between">
				<view>{{item.name}}</view>
				<view>{{item.addtime}}</view>
			</view>
			<view class="flex align between info">
				<view>绑定视频数</view>
				<view>{{item.video_count}}</view>
				<view class="flex align">
					<uni-icons @click="toEdit(item.name,item.id)" type="compose" size="20" color="#D81E06"
						style="margin-right: 30rpx;"></uni-icons>
					<uni-icons @click="toDel(item.id)" type="trash" size="20" color="#D81E06"></uni-icons>
				</view>
			</view>
			<view class="look" v-if="item.video_count>0" @click="toUrl('/pages/videoProdue/group/look?id='+item.id)">查看</view>
		</view>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<u-popup :show="show" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">添加</view>
				<view class="title">分组名</view>
				<input placeholder="请输入分组名，建议2-30个字" v-model="name" />
				<view class="subBtn" @click="toAdd">确定</view>
			</view>
		</u-popup>
		<u-popup :show="show2" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show2 = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">编辑</view>
				<view class="title">分组名</view>
				<input placeholder="请输入分组名，建议2-30个字" v-model="nameEdit" />
				<view class="subBtn" @click="toEditCom">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		tuiguangVideoGroup,
		tuiguangVideoGroupAdd,
		tuiguangVideoGroupEdit,
		tuiguangVideoGroupDel
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				searchValue: '',
				show: false,
				show2: false,
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				name: '',
				nameEdit: '',
				idEdit: '',
				idDel:''
			};
		},
		onShow() {
			this.page = 0
			this.list = []
			this.getlist();
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			toDel(id){
				this.idDel = id
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除该分组吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.toDelCom()
						} else {
						
						}
					}
				
				})
			},
			async toDelCom(){
				let obj = {
					action:'del',
					ids:this.idDel
				}
				const result = await tuiguangVideoGroupDel(obj)
				if (result.code == 1) {
					uni.showToast({
						title: '删除成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist();
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			},
			toEdit(name, id) {
				this.nameEdit = name
				this.idEdit = id
				this.show2 = true
			},
			toEditCom() {
				let obj = {
					'row[name]': this.nameEdit
				}
				console.log(obj)
				let that = this
				uni.request({
					url: 'https://jzxt.jiugezixun.cn/storeadmin/tuiguang_video_group/edit/ids/' + that.idEdit,
					data: obj,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'XMLHttpRequest',
						'Cookie': uni.getStorageSync('cookies') // 从存储中读取cookies并设置到请求头
					},
					success: result => {
						console.log(result)
						if (result.data.code == 1) {
							uni.showToast({
								title: '修改成功'
							})
							that.show2 = false
							that.page = 0
							that.list = []
							that.getlist();
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none'
							})
						}
					},
					fail: result => {

					}
				})
				// const result = await tuiguangVideoGroupEdit(obj)
				// if (result.code == 1) {
				// 	uni.showToast({
				// 		title: '修改成功'
				// 	})
				// 	this.show = false
				// 	this.page = 0
				// 	this.list = []
				// 	this.getlist();
				// } else {
				// 	uni.showToast({
				// 		title: result.msg,
				// 		icon: 'none'
				// 	})
				// }
			},
			async toAdd() {
				let obj = {
					'row[name]': this.name
				}
				console.log(obj)
				const result = await tuiguangVideoGroupAdd(obj)
				if (result.code == 1) {
					uni.showToast({
						title: '添加成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist();
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}

			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					searchValue
				} = that;
				let filter = {}
				if (searchValue != '') {
					filter = {
						"title": searchValue
					}
				}
				let obj = {
					menu_url: 'tuiguang_video_group',
					limit: offset,
					offset: page,
					filter: JSON.stringify(filter),
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await tuiguangVideoGroup(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			toUrl(u) {
				uni.navigateTo({
					url: u
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		margin: 13rpx 30rpx;

		.topSearch {
			.addBtn {
				background: #FFCDB8;
				color: white;
				border-radius: 14rpx;
				font-size: 20rpx;
				padding: 8rpx 14rpx;
			}
		}

		.list {
			background: white;
			box-shadow: 1rpx 1rpx 5rpx 1rpx gainsboro;
			border-radius: 15rpx;
			margin-bottom: 50rpx;

			.name {
				margin: 0 17rpx;
				padding: 26rpx 15rpx 8rpx;
				border-bottom: solid 1rpx #D6D6D6;

				view:nth-child(1) {
					font-size: 28rpx;
					font-weight: bold;
				}

				view:nth-child(2) {
					font-size: 18rpx;
					color: #868686;
				}
			}

			.info {
				font-size: 24rpx;
				padding: 14rpx 41rpx;
			}

			.look {
				border-radius: 0 0 15rpx 15rpx;
				text-align: center;
				color: white;
				font-weight: bold;
				font-size: 32rpx;
				padding: 12rpx 0;
				background: #2D2D2D;
			}
		}

		.btomShow {
			padding: 0 28rpx 150rpx;
			position: relative;

			.showTit {
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				padding: 33rpx 0;
			}

			input {
				font-size: 20rpx;
				padding: 16rpx 24rpx;
				border-radius: 10rpx;
				border: solid 1rpx #707070;
			}

			.closeempty {
				position: absolute;
				top: 20rpx;
				right: 30rpx;

			}

			.title {
				font-size: 28rpx;
				margin: 25rpx 0;
			}

			.subBtn {
				font-size: 30rpx;
				color: white;
				padding: 8rpx 95rpx;
				background: #5D9CEB;
				width: fit-content;
				margin: 137rpx auto 0rpx;
				border-radius: 10rpx;
			}
		}
	}
</style>